/*
 * This packet is the response from the server to the client for the security
 * level setting request.
 *
 * @author Jonathan Walsh (jwalsh8484@gmail.com)
 */

package egs.packets;

import java.nio.ByteBuffer;

public class UseSecureResponsePacket extends Packet {
    
    public enum ResponseCode {
        accepted ((short)0), 
            unsupported_protocol ((short)1), 
            connection_error ((short)2);
        
        public final short value;
        ResponseCode( short value ) {
            this.value = value;
        };
    };
    
    private ResponseCode response_code;
    private short securePort = 0;
    
    public UseSecureResponsePacket( ResponseCode response_code ) {
        this.response_code = response_code;
               
        /*Set up the header*/
        message_type = MSG_USE_SECURE_RESPONSE;
        game_name = 0;
        game_version = 0;
        data_area_size = 4; /*only two shorts in this packet*/
    }
    
    public UseSecureResponsePacket( byte[] raw ) {
        ByteBuffer buf = setHeaderNew( raw );
        short val_from_packet = buf.getShort( HEADER_SIZE );
        response_code = ResponseCode.class.getEnumConstants()[val_from_packet];
        val_from_packet = buf.getShort(HEADER_SIZE + 2);
        this.securePort = val_from_packet;
    }
    
    public byte[] getRaw() {
        ByteBuffer buf = makeBufferWithHeader( data_area_size );
        buf.putShort( response_code.value );
        buf.putShort( this.securePort );
        
        return buf.array();
    }
    
    public String toString() {
        return super.toString() + "\nresponse_code = " + response_code.value;
    }
    
    public ResponseCode get_response_code() { return response_code; }
    
    public void setPort(short port){
    	this.securePort = port;
    }
    
    public short getPort(){
    	return this.securePort;
    }
    
}
